<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="模板标题" prop="templateTitle">
      <el-input v-model="dataForm.templateTitle" placeholder="模板标题"></el-input>
    </el-form-item>
    <el-form-item label="模板类型" prop="templateType">
      <el-radio v-model="dataForm.templateType" :label="1">zip压缩包</el-radio>
      <el-radio v-model="dataForm.templateType" :label="2">git仓库</el-radio>
    </el-form-item>
    <el-form-item v-if="dataForm.templateType === 1" label="压缩包名称" prop="zipName">
      <el-input v-model="dataForm.zipName" placeholder="模板zip压缩包名称"></el-input>
    </el-form-item>
    <template v-if="dataForm.templateType === 2">
      <el-form-item label="git仓库地址" prop="gitAddress">
        <el-input v-model="dataForm.gitAddress" placeholder="git仓库地址"></el-input>
      </el-form-item>
      <el-form-item label="仓库状态" prop="isAuth">
        <el-radio v-model="dataForm.isAuth" :label="1">私有</el-radio>
        <el-radio v-model="dataForm.isAuth" :label="2">公共</el-radio>
      </el-form-item>
      <template v-if="dataForm.isAuth === 1">
        <el-form-item label="git账号" prop="gitUsername">
          <el-input v-model="dataForm.gitUsername" placeholder="git账号"></el-input>
        </el-form-item>
        <el-form-item label="git密码" prop="gitPassword">
          <el-input v-model="dataForm.gitPassword" show-password placeholder="git密码"></el-input>
        </el-form-item>
      </template>
    </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          templateTitle: '',
          templateType: 0,
          zipName: '',
          gitAddress: '',
          gitUsername: '',
          gitPassword: '',
          isAuth: 0,
          createUserId: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          templateTitle: [
            { required: true, message: '模板标题不能为空', trigger: 'blur' }
          ],
          templateType: [
            { required: true, message: '模板类型(1-zip压缩包,2-git仓库)不能为空', trigger: 'blur' }
          ],
          zipName: [
            { required: true, message: '模板zip压缩包名称不能为空', trigger: 'blur' }
          ],
          gitAddress: [
            { required: true, message: 'git仓库地址不能为空', trigger: 'blur' }
          ],
          gitUsername: [
            { required: true, message: 'git账号不能为空', trigger: 'blur' }
          ],
          gitPassword: [
            { required: true, message: 'git密码不能为空', trigger: 'blur' }
          ],
          isAuth: [
            { required: true, message: 'git地址是否需要密码不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/config/template/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.templateTitle = data.data.templateTitle
                this.dataForm.templateType = data.data.templateType
                this.dataForm.zipName = data.data.zipName
                this.dataForm.gitAddress = data.data.gitAddress
                this.dataForm.gitUsername = data.data.gitUsername
                this.dataForm.gitPassword = data.data.gitPassword
                this.dataForm.isAuth = data.data.isAuth
                this.dataForm.createUserId = data.data.createUserId
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateTime = data.data.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/config/template/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'templateTitle': this.dataForm.templateTitle,
                'templateType': this.dataForm.templateType,
                'zipName': this.dataForm.zipName,
                'gitAddress': this.dataForm.gitAddress,
                'gitUsername': this.dataForm.gitUsername,
                'gitPassword': this.dataForm.gitPassword,
                'isAuth': this.dataForm.isAuth,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
